package com.xb.controller;

import com.xb.entity.Dict;
import com.xb.service.IDictService;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * entityController
 * /api/dict
 */
@RestController
@RequestMapping("/api/dict")
public class DictController {
    @Autowired
    private IDictService dictService;

    @GetMapping("/getDict")
    public Result getDict(String typeNo) {
        return dictService.getDict(typeNo);
    }

    @RequestMapping("/page")
    @RequiresPermissions("system:dict:page")
    public Object findPage(Dict obj, @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        return dictService.selectDictList(obj, page, limit);
    }

    @DeleteMapping("/{id}")
    @RequiresPermissions("system:dict:del")
    public Object del(@PathVariable Integer id) {
        Result result = new Result();
        if (dictService.deleteDictById(id) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("删除成功！");
        } else {
            result.setMessage("删除失败！");
        }
        return result;
    }

    @PostMapping
    public Result addDict(@RequestBody Dict dict) {
        Result result = new Result();
        if (dictService.insertDict(dict) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("添加成功！");
        }else{
            result.setMessage("添加失败！");
        }
        return result;
    }

    @PutMapping
    public Result editDict(@RequestBody Dict dict) {
        Result result = new Result();
        if(dictService.updateDict(dict)>0){
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("修改成功！");
        }else{
            result.setMessage("修改失败！");
        }
        return result;
    }
}
